package com.ruoyi.project.modules.interfaceinfo.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.errorCodeCategory.domain.ErrorCodeCategory;
import com.ruoyi.project.modules.errorCodeCategory.service.IErrorCodeCategoryService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 虚拟用况接口Controller
 *
 * @author smallrain
 * @date 2021-10-29
 */
@Controller
@RequestMapping("/interfaceinfo/virtualUsage")
public class VirtualUsageController extends BaseController {
    private String prefix = "interface/virtualUsage";

    @Autowired
    private IVirtualUsageService virtualUsageService;

    @RequiresPermissions("interfaceinfo:virtualUsage:view")
    @GetMapping()
    public String virtualUsage() {
        return prefix + "/virtualUsage";
    }

    /**
     * 查询虚拟用况接口列表
     */
    @RequiresPermissions("interfaceinfo:virtualUsage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualUsage virtualUsage) {
        startPage();
        List<VirtualUsage> list = virtualUsageService.selectVirtualUsages(virtualUsage);
        return getDataTable(list);
    }

    /**
     * 导出虚拟用况接口列表
     */
    @RequiresPermissions("interfaceinfo:virtualUsage:export")
    @Log(title = "虚拟用况接口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualUsage virtualUsage) {
        List<VirtualUsage> list = virtualUsageService.selectVirtualUsageList(virtualUsage);
        ExcelUtil<VirtualUsage> util = new ExcelUtil<VirtualUsage>(VirtualUsage.class);
        return util.exportExcel(list, "虚拟用况接口数据");
    }

    /**
     * 新增虚拟用况接口
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存虚拟用况接口
     */
    @RequiresPermissions("interfaceinfo:virtualUsage:add")
    @Log(title = "虚拟用况接口", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualUsage virtualUsage) {
        return toAjax(virtualUsageService.insertVirtualUsage(virtualUsage));
    }

    /**
     * 修改虚拟用况接口
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VirtualUsage virtualUsage = virtualUsageService.selectVirtualUsageById(id);
        mmap.put("virtualUsage", virtualUsage);
        return prefix + "/edit";
    }

    /**
     * 修改保存虚拟用况接口
     */
    //@RequiresPermissions("interfaceinfo:virtualUsage:edit")
    @Log(title = "虚拟用况接口", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualUsage virtualUsage) {
        return toAjax(virtualUsageService.updateVirtualUsage(virtualUsage));
    }

    /**
     * 删除虚拟用况接口
     */
    //@RequiresPermissions("interfaceinfo:virtualUsage:remove")
    @Log(title = "虚拟用况接口", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualUsageService.deleteVirtualUsageByIds(ids));
    }

    @GetMapping("/setParam")
    public String setParam(@RequestParam Long id, @RequestParam Integer interfaceType, ModelMap mmap) {
        VirtualUsage param = new VirtualUsage();
        param.setId(id);
        List<VirtualUsage> list = virtualUsageService.selectVirtualUsages(param);
        if (CollectionUtil.isNotEmpty(list)) {
            VirtualUsage value = list.get(0);
            value.setInterfaceType(interfaceType);
            mmap.put("virtualUsage", value);
        }
        return prefix + "/setParam";
    }

    @PostMapping("/saveParam")
    @ResponseBody
    public AjaxResult saveParam(InterfaceInfo interfaceInfo) {
        this.virtualUsageService.saveVirtualUsage(interfaceInfo);
        return success();
    }

    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private IErrorCodeCategoryService errorCodeCategoryService;

    @GetMapping("/virtualUsageModelSelect")
    public String virtualUsageModelSelect(Long id, ModelMap modelMap) {
        VirtualUsage virtualUsage = this.virtualUsageService.selectVirtualUsageById(id);
        modelMap.put("virtualUsage", virtualUsage);

        List<SysDictData> dictCategoryList = new ArrayList<>();
        List<SysDictData> dictValueList = new ArrayList<>();
        List<SysDictType> dictTypeList = new ArrayList<>();
        List<SysDictData> dataDictionary = this.sysDictTypeService.selectDictDataByType("dataDictionary");
        for (SysDictData sysDictData : dataDictionary) {
            //if (!"1".equals(sysDictData.getDictValue()) && !"2".equals(sysDictData.getDictValue())) {
            if (!"1".equals(sysDictData.getDictValue())) {
                dictCategoryList.add(sysDictData);
            }
        }
        if (StringUtils.isNotEmpty(virtualUsage.getDictCategory())) {
            SysDictType dictType1 = new SysDictType();
            dictType1.setCategory(virtualUsage.getDictCategory());
            dictTypeList = this.sysDictTypeService.selectDictTypeList(dictType1);
        }
        if (StringUtils.isNotEmpty(virtualUsage.getDictType())) {
            dictValueList = this.sysDictTypeService.selectDictDataByType(virtualUsage.getDictType());
        }
        if (virtualUsage.getErrorCodeCategoryId() != null) {
            ErrorCodeCategory errorCodeCategory = this.errorCodeCategoryService.selectErrorCodeCategoryByErrorCodeCategoryId(virtualUsage.getErrorCodeCategoryId());
            modelMap.put("errorCodeCategoryName", errorCodeCategory.getErrorCodeCategoryName());
        }


        modelMap.put("dictCategoryList", dictCategoryList);
        modelMap.put("dictValueList", dictValueList);
        modelMap.put("dictTypeList", dictTypeList);
        modelMap.put("dictCategory", StringUtils.isNotEmpty(virtualUsage.getDictCategory()) ? virtualUsage.getDictCategory() : "");
        modelMap.put("dictType",  StringUtils.isNotEmpty(virtualUsage.getDictType()) ? virtualUsage.getDictType() : "");
        modelMap.put("selfModel",  StringUtils.isNotEmpty(virtualUsage.getSelfModel()) ? virtualUsage.getSelfModel() : "");
        return "interface/virtualUsage/virtualUsageModelSelect";
    }

    @PostMapping("/editModel")
    @ResponseBody
    public AjaxResult editModel(VirtualUsage virtualUsage) {
        if (!"1".equals(virtualUsage.getIsAllowEmpty())) {
            virtualUsage.setErrorCode(null);
            virtualUsage.setErrorCodeCategoryId(null);
        }
        return toAjax(virtualUsageService.updateVirtualUsageModel(virtualUsage));
    }

    @PostMapping("/updateMaster")
    @ResponseBody
    public AjaxResult updateMaster(VirtualUsage virtualUsage) {
        VirtualUsage usage = this.virtualUsageService.selectVirtualUsageById(virtualUsage.getId());
        usage.setIsMaster(virtualUsage.getIsMaster());
        if ("Y".equals(virtualUsage.getIsMaster())) {
            VirtualUsage vu = new VirtualUsage();
            vu.setRelationInterfaceId(usage.getRelationInterfaceId());
            vu.setIsMaster("Y");
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(vu);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                return AjaxResult.warn("其它虚拟用况已设置主");
            }
        }
        this.virtualUsageService.updateVirtualUsage(usage);
        return success();
    }
}
